<?php
namespace app\home\controller;

use think\Controller;
use think\Db;

class Lottery extends Controller
{
    public function index()
    {
        return $this->view->fetch();
    }

    public function getRe(){
        $lotteryInfo = Db::name('Lottery')->where('lottery_id', 1)->find();
        $prizeArr = Db::name('LotteryPrize')->where(['lottery_id'=>$lotteryInfo['lottery_id'], 'number_left'=>['>', 0]])->select();
        if(!$prizeArr){
            return ajax_return_error('奖品已被抽完,感谢您参与本次活动');
        }

        //获取中奖奖品ID
        $lottery = new \Lottery();
        $prize_id = $lottery->start($prizeArr);

        //更新库存
        $prizeInfo = Db::name('LotteryPrize')->where('prize_id', $prize_id)->find();
        $number_left = $prizeInfo['number_left']-1;
        $data = ['number_left' => $number_left];
        $res = Db::name('LotteryPrize')->where('prize_id', $prizeInfo['prize_id'])->update($data);
        $prizeInfo['angle'] = mt_rand($prizeInfo['min'], $prizeInfo['max']);
        if($res > 0){
            return ajax_return('', $prizeInfo);
        }else{
            return ajax_return_error('抽奖出了点小问题，请联系客服处理');
        }
    }
}
